// Copyright 叶湘 ( weixin:yexiang841, email:yexiang841@qq.com ), Shitouren Studio. All Rights Reserved.

#include "GameState/Q3D_CPPA_GameState.h"
#include "PlayerState/Q3D_CPPA_PlayerState.h"
#include "GameFramework/SpectatorPawn.h"
#include "Logger/Q3D_CPPU_Logger.h"

AQ3D_CPPA_GameState::AQ3D_CPPA_GameState()
{
    Q3D_LOG(Display, "GameState_Constructed");
}

AQ3D_CPPA_GameState::~AQ3D_CPPA_GameState()
{
}

void AQ3D_CPPA_GameState::Q3D_F_PawnState_Inc(UQ3D_CPPU_PawnState *ptr_u_pawn_state, bool &bool_success, FString &string_log)
{
    // 参数检查
    if (!ptr_u_pawn_state)
    {
        bool_success = false;
        string_log = FString("Invalid_Ptr_U_PawnState");
        Q3D_LOG(Error, string_log);
        return;
    }
    // 越界检查
    int32 int_player_index_tmp;
    ptr_u_pawn_state->Q3D_F_PlayerIndex_Get(int_player_index_tmp);
    if (int_player_index_tmp < 0 || int_player_index_tmp >= this->PlayerArray.Num())
    {
        bool_success = false;
        string_log = "PlayerIndex_[" + FString::FromInt(int_player_index_tmp) + "]_Out_Of_Len_[" + FString::FromInt(this->PlayerArray.Num()) + "]";
        Q3D_LOG(Error, string_log);
        return;
    }
    AQ3D_CPPA_PlayerState *ptr_a_player_state = Cast<AQ3D_CPPA_PlayerState>(this->PlayerArray[int_player_index_tmp]);
    // PlayerState 类型检查
    if (!ptr_a_player_state)
    {
        bool_success = false;
        string_log = FString("Cast_To_Q3D_CPPA_PlayerState_Failed");
        Q3D_LOG(Error, string_log);
        return;
    }
    // 日志
    // Q3D_LOG( Verbose, "Inc_[" + ptr_u_pawn_state->String_NickName + "]_To_Player_[" + FString::FromInt( int_player_index_tmp ) + "]" );
    // 调用并设置返回值
    ptr_a_player_state->Q3D_F_PawnState_Inc(ptr_u_pawn_state, bool_success, string_log);
    return;
}

void AQ3D_CPPA_GameState::Q3D_F_PawnState_Dec(UQ3D_CPPU_PawnState *ptr_u_pawn_state, bool &bool_success, FString &string_log, UQ3D_CPPU_PawnState *&ptr_u_pawn_state_next)
{
    // 参数检查
    if (!ptr_u_pawn_state)
    {
        bool_success = false;
        string_log = FString("Invalid_Ptr_U_PawnState");
        Q3D_LOG(Error, string_log);
        return;
    }
    // 越界检查
    int32 int_player_index_tmp;
    ptr_u_pawn_state->Q3D_F_PlayerIndex_Get(int_player_index_tmp);
    if (int_player_index_tmp < 0 || int_player_index_tmp >= this->PlayerArray.Num())
    {
        bool_success = false;
        string_log = "PlayerIndex_[" + FString::FromInt(int_player_index_tmp) + "]_Out_Of_Len_[" + FString::FromInt(this->PlayerArray.Num()) + "]";
        Q3D_LOG(Error, string_log);
        return;
    }
    AQ3D_CPPA_PlayerState *ptr_a_player_state = Cast<AQ3D_CPPA_PlayerState>(this->PlayerArray[int_player_index_tmp]);
    // PlayerState 类型检查
    if (!ptr_a_player_state)
    {
        bool_success = false;
        string_log = FString("Cast_To_Q3D_CPPA_PlayerState_Failed");
        Q3D_LOG(Error, string_log);
        return;
    }
    // 日志
    Q3D_LOG(Log, "Dec_[" + ptr_u_pawn_state->String_NickName + "]_To_Player_[" + FString::FromInt(int_player_index_tmp) + "]");
    // 调用并设置返回值
    ptr_a_player_state->Q3D_F_PawnState_Dec(ptr_u_pawn_state, bool_success, string_log, ptr_u_pawn_state_next);
    return;
}

void AQ3D_CPPA_GameState::Q3D_F_PawnState_Spectator_Set(UQ3D_CPPU_PawnState *ptr_u_pawn_state, bool &bool_success, FString &string_log)
{
    // 参数检查
    if (!ptr_u_pawn_state)
    {
        bool_success = false;
        string_log = FString("Invalid_Ptr_U_PawnState");
        Q3D_LOG(Error, string_log);
        return;
    }
    // 越界检查
    int32 int_player_index_tmp;
    ptr_u_pawn_state->Q3D_F_PlayerIndex_Get(int_player_index_tmp);
    if (int_player_index_tmp < 0 || int_player_index_tmp >= this->PlayerArray.Num())
    {
        bool_success = false;
        string_log = "PlayerIndex_[" + FString::FromInt(int_player_index_tmp) + "]_Out_Of_Len_[" + FString::FromInt(this->PlayerArray.Num()) + "]";
        Q3D_LOG(Error, string_log);
        return;
    }
    // SpectatorPawn 类型检查
    ASpectatorPawn *ptr_a_spectator_pawn = Cast<ASpectatorPawn>(ptr_u_pawn_state->Ptr_A_Pawn);
    if (!ptr_a_spectator_pawn)
    {
        bool_success = false;
        string_log = FString("Cast_To_Q3D_CPPA_SpectatorPawn_Failed");
        Q3D_LOG(Error, string_log);
        return;
    }
    AQ3D_CPPA_PlayerState *ptr_a_player_state = Cast<AQ3D_CPPA_PlayerState>(this->PlayerArray[int_player_index_tmp]);
    // PlayerState 类型检查
    if (!ptr_a_player_state)
    {
        bool_success = false;
        string_log = FString("Cast_To_Q3D_CPPA_PlayerState_Failed");
        Q3D_LOG(Error, string_log);
        return;
    }
    // 日志
    Q3D_LOG(Verbose, "Set_Spectator_[" + ptr_u_pawn_state->String_NickName + "]_To_Player_[" + FString::FromInt(int_player_index_tmp) + "]");
    // 调用并设置返回值
    ptr_a_player_state->Q3D_F_PawnState_Spectator_Set(ptr_u_pawn_state, bool_success, string_log);
    return;
}

void AQ3D_CPPA_GameState::Q3D_F_PawnState_Spectator_Unset(int32 int_player_index, bool &bool_success, FString &string_log)
{
    // 越界检查
    if (int_player_index < 0 || int_player_index >= this->PlayerArray.Num())
    {
        bool_success = false;
        string_log = "PlayerIndex_[" + FString::FromInt(int_player_index) + "]_Out_Of_Len_[" + FString::FromInt(this->PlayerArray.Num()) + "]";
        Q3D_LOG(Error, string_log);
        return;
    }
    AQ3D_CPPA_PlayerState *ptr_a_player_state = Cast<AQ3D_CPPA_PlayerState>(this->PlayerArray[int_player_index]);
    // PlayerState 类型检查
    if (!ptr_a_player_state)
    {
        bool_success = false;
        string_log = FString("Cast_To_Q3D_CPPA_PlayerState_Failed");
        Q3D_LOG(Error, string_log);
        return;
    }
    // 日志
    Q3D_LOG(Verbose, "Unset_Spectator_To_Player_[" + FString::FromInt(int_player_index) + "]");
    // 调用并设置返回值
    ptr_a_player_state->Q3D_F_PawnState_Spectator_Unset(bool_success, string_log);
    return;
}

void AQ3D_CPPA_GameState::Q3D_F_PawnState_Spectator_Get(int32 int_player_index, UQ3D_CPPU_PawnState *&ptr_u_pawn_state_spectator) const
{
    // 越界检查
    if (int_player_index < 0 || int_player_index >= this->PlayerArray.Num())
    {
        ptr_u_pawn_state_spectator = nullptr;
        Q3D_LOG(Error, "PlayerIndex_[" + FString::FromInt(int_player_index) + "]_Out_Of_Len_[" + FString::FromInt(this->PlayerArray.Num()) + "]");
        return;
    }
    AQ3D_CPPA_PlayerState *ptr_a_player_state = Cast<AQ3D_CPPA_PlayerState>(this->PlayerArray[int_player_index]);
    // PlayerState 类型检查
    if (!ptr_a_player_state)
    {
        ptr_u_pawn_state_spectator = nullptr;
        Q3D_LOG(Error, FString("Cast_To_Q3D_CPPA_PlayerState_Failed"));
        return;
    }
    // 返回值
    ptr_u_pawn_state_spectator = ptr_a_player_state->Ptr_U_PawnState_Spectator;
    return;
}
